//
//  MyPointsHeadCard.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/11/7.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MyPointsHeadCard.h"
#import "MineTopBottomItem.h"
#import "MineCountModel.h"
@interface MyPointsHeadCard ()
@property(nonatomic, strong) UIImageView *bgImageView;
@property(nonatomic, strong) NSMutableArray<MineItemModel *> *itemModels;
@property(nonatomic, strong) NSArray<MineTopBottomItem *> *items;
@end

@implementation MyPointsHeadCard
- (void)makeUI {
    [super makeUI];
    self.backgroundColor = UIColor.clearColor;
    [self addSubview:self.bgImageView];
    
    {
        NSArray *titles = @[@"可用积分", @"冻结积分"];
        NSArray<NSNumber *> *actions = @[@(ItemActionAvailbleCredit), @(ItemActionFreezeCredits)];
        NSMutableArray<MineItemModel *> *itemModels = [NSMutableArray array];
        NSMutableArray<MineTopBottomItem *> *items = [NSMutableArray array];
        CGFloat orinY = 16;
        for (NSInteger index = 0; index < titles.count; index ++) {
            MineItemModel *item = [[MineItemModel alloc]init];
            item.style = ItemStyleCredit;
            item.action = actions[index];
            
            item.title = titles[index];
            item.subTitle = @"0";
            item.titleFont = kFontRegular(12);
            item.titleColor = UIColor.whiteColor;
            
            item.subTitleFont = kFontBold(18);
            item.subtTitleColor = UIColor.whiteColor;
            
            item.itemMargin = 4;
            
            [itemModels addObject:item];
            
            if (index == 1) {
                item.hasTips = YES;
            }
        }
        CGFloat itemWidth = ceil((self.width - 44) / titles.count);
        for (NSInteger index = 0; index < itemModels.count; index ++) {
            MineTopBottomItem *item = [[MineTopBottomItem alloc]initWithFrame:CGRectMake(itemWidth * index + 35 * (index + 1), orinY, itemWidth, self.height - 20)];
            [self.bgImageView addSubview:item];
            item.data = itemModels[index];
            // items
            [items addObject:item];
            if (index == 1) {
                WEAKSELF
                [item setClickImageViewCallBackImageViewFrame:^(MineTopBottomItem * _Nonnull selfView, CGRect frame) {
                    CGRect tempFrame = [weakSelf convertRect:frame fromView:selfView];
                    !weakSelf.clickImageViewCallBackImageViewFrame ?: weakSelf.clickImageViewCallBackImageViewFrame(tempFrame);
                }];
            }
        }
        [self.itemModels addObjectsFromArray:itemModels];
        self.items = items;
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.bgImageView.frame = self.bounds;
}
- (void)setCountModel:(MineCountModel *)countModel {
    _countModel = countModel;
    for (MineTopBottomItem *item in self.items) {
        if (item.data.action.integerValue == ItemActionAvailbleCredit) {
            item.data.subTitle = [NSString stringWithFormat:@"%@", [Utilities removeNumberZeroWithDouble:countModel.availableCredits.doubleValue]];
        } else if (item.data.action.integerValue == ItemActionFreezeCredits) {
            item.data.subTitle = [NSString stringWithFormat:@"%@", [Utilities removeNumberZeroWithDouble:countModel.freezeCredits.doubleValue]];
        }
        item.data = item.data;
    }
}
#pragma mark - lazy
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.image = [UIImage imageNamed:ImageName.credit_banner_bg];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        iv.userInteractionEnabled = YES;
        _bgImageView = iv;
    }
    return _bgImageView;
}
@end
